package com.medic.client.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.medic.entity.Doctor;
import com.medic.entity.User;
import com.medic.entity.Weichat;
import com.medic.entity.group.pain.Alert;
import com.medic.entity.group.pain.Medic;
import com.medic.service.impl.MedicServiceImpl;
import com.medic.service.impl.UserServiceImpl;
import com.medic.service.impl.WeichatServiceImpl;
import com.medic.util.CacheUtil;
import com.medic.util.LoggerUtil;
import com.medic.util.StringUtil;

@Controller
@RequestMapping("/client/user")
@SessionAttributes({ "user", "weichat" })
public class UserController {

	@Resource(name = "userService")
	private UserServiceImpl userService;

	@Resource(name = "weichatService")
	private WeichatServiceImpl weichatService;

	@Resource(name = "medicService")
	private MedicServiceImpl medicService;

	@RequestMapping("")
	public String user(String res) {
		String result = "/client/" + res;
		return result;
	}

	@RequestMapping("/updatePhone")
	@ResponseBody
	public String update(@ModelAttribute("user") User user, String phone) {
		String result = "";
		try {
			User object = new User();
			object.setPhone(phone);
			object.setId(user.getId());
			LoggerUtil.debug(phone + "-----------------" + user.getId());
			if (userService.update(object)) {
				user.setPhone(phone);
				result = "{\"result\":\"success\"}";
			} else {
				result = "{\"result\":\"fail\"}";
			}
		} catch (Exception e) {
			LoggerUtil.error("error : " + e.getMessage() + " - params : [ userId : " + user.getId() + ",phone : "
					+ phone + " ]");
			result = "{\"result\":\"error\"}";
		}
		return result;
	}

	public String search(@ModelAttribute(StringUtil.DOCTOR_KEY) Doctor doctor, int groupId, String username,
			Model model) {
		String result = "";
		try {
			if (groupId != 0) {

			}
		} catch (Exception e) {

		}
		return result;
	}

	@RequestMapping("register")
	public String register(User user, Model model) {
		String result = "";
		try {
			userService.save(user);
			List<Weichat> list = weichatService.getRoleByOpenID(user.getOpenid());
			for (Weichat weichat : list) {
				if (weichat.getRole().equals(StringUtil.USER_KEY)) {
					model.addAttribute(StringUtil.WECHAT_KEY, weichat);
					break;
				}
			}
			Medic medic = new Medic();
			medic.setMedicName(StringUtil.MEDIC_NAME);
			medic.setMedicUnit("mg");
			medic.setOpenId(user.getOpenid());
			medic.setSikerId(user.getId());
			medic.setTakeType("口服");
			medicService.save(medic);
			CacheUtil.getMedics().put(medic.getMedicId(), medic);
			model.addAttribute(StringUtil.USER_KEY, user);
			result = "redirect:/client/index?msg=user-index";
		} catch (Exception e) {
			LoggerUtil.error("error : " + e.getMessage() + " - params : [ user : " + user.toString() + " ]");
			result = "error";
		}
		return result;
	}
}
